package action.config;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import component.RunCmdComponent;
import constants.Constants;
import utils.GameUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther syc.
 * @Date 2016/11/16 16:49.
 */
public class ConfigEffectAction extends AnAction {
    @Override
    public void actionPerformed(AnActionEvent anActionEvent) {
        Application application = ApplicationManager.getApplication();
        RunCmdComponent component = application.getComponent(RunCmdComponent.class);


        String projectPath = GameUtils.getPropertyValue(anActionEvent, Constants.PROJECT_PATH_PRO_NAME);
        if (null == projectPath || projectPath.equals("")) {
            GameUtils.log("请配置工程路径：路径配置->工程路径");
            return;
        }

        String oldJSONPath = GameUtils.getPropertyValue(anActionEvent, Constants.CONFIG_JSON_EFFECT_PATH_PRO_NAME);
        if (null == oldJSONPath || oldJSONPath.equals("")) {
            String defaultJSONEffectPath = projectPath + Constants.RELATIVE_JSON_EFFECT_PATH;
            GameUtils.setGameProperties(anActionEvent, Constants.CONFIG_JSON_EFFECT_PATH_PRO_NAME, defaultJSONEffectPath, "set the default effect path");
            GameUtils.log("配置JSON数据生效路径配置默认为：" + defaultJSONEffectPath + "\n如需请配置数据配置生效路径：数据配置->配置JSON数据生效路径配置");
        }

        String oldJAVAPath = GameUtils.getPropertyValue(anActionEvent, Constants.CONFIG_JAVA_EFFECT_PATH_PRO_NAME);
        if (null == oldJAVAPath || oldJAVAPath.equals("")) {
            String defaultJAVAEffectPath = projectPath + Constants.RELATIVE_EFFECT_PATH + File.separator + "config";
            GameUtils.setGameProperties(anActionEvent, Constants.CONFIG_JAVA_EFFECT_PATH_PRO_NAME, defaultJAVAEffectPath, "set the default effect path");
            GameUtils.log("配置JAVA数据生效路径配置默认为：" + defaultJAVAEffectPath + "\n如需请配置数据配置生效路径：数据配置->配置JAVA数据生效路径配置");
        }

        String oldStagePath = GameUtils.getPropertyValue(anActionEvent, Constants.CONFIG_STAGE_EFFECT_PATH_PRO_NAME);
        if (null == oldStagePath || oldStagePath.equals("")) {
            String defaultStageEffectPath = projectPath + Constants.RELATIVE_STAGE_EFFECT_PATH;
            GameUtils.setGameProperties(anActionEvent, Constants.CONFIG_STAGE_EFFECT_PATH_PRO_NAME, defaultStageEffectPath, "set the default effect path");
            GameUtils.log("配置Stage数据生效路径配置默认为：" + defaultStageEffectPath + "\n如需请配置数据配置生效路径：数据配置->配置Stage数据生效路径配置");
        }

        String configPath = GameUtils.getPropertyValue(anActionEvent, Constants.CONFIG_PATH_PRO_NAME);
        if (null == configPath || configPath.equals("")) {
            GameUtils.log("请配置数据配置路径：路径配置->数据配置路径");
            return;
        }


        List oldToNewFilePaths = createListOfPathPairs(anActionEvent, configPath, Constants.CONFIG_JAVA_GEN_DIR_PATH ,Constants.CONFIG_JSON_GEN_DIR_PATH);

//        GameUtils.log("配置config数据生效路径配置默认为：" + oldToNewFilePaths);

        // json文件生效
        String jsonOldPath = configPath + Constants.CONFIG_JSON_GEN_DIR_PATH;
        String jsonNewPath = GameUtils.getPropertyValue(anActionEvent, Constants.CONFIG_JSON_EFFECT_PATH_PRO_NAME);
        component.moveFile(anActionEvent, "configJSONEffect", jsonOldPath, jsonNewPath);

        // java文件生效
        String javaOldPath = configPath + Constants.CONFIG_JAVA_GEN_DIR_PATH;
        String javaNewPath = GameUtils.getPropertyValue(anActionEvent, Constants.CONFIG_JAVA_EFFECT_PATH_PRO_NAME);
        component.moveFile(anActionEvent, "configJAVAEffect", javaOldPath, javaNewPath);

        // stage文件生效
        component.copyFile(anActionEvent, "stageEffect", configPath + Constants.CONFIG_STAGE_GEN_DIR_PATH ,GameUtils.getPropertyValue(anActionEvent, Constants.CONFIG_STAGE_EFFECT_PATH_PRO_NAME));

//        GameUtils.log("配置stage数据生效路径配置默认为：" +configPath + Constants.CONFIG_STAGE_GEN_DIR_PATH+ "--->" + GameUtils.getPropertyValue(Constants.CONFIG_STAGE_EFFECT_PATH_PRO_NAME));


    }

    private List createListOfPathPairs(AnActionEvent anActionEvent, String configPath, String configJAVAGenDirPath, String configJSONGenDirPath) {
        List<List<String>> oldToNewFilePaths = new ArrayList<>();

        List<String> JOSNOldToNew = new ArrayList<>();
        JOSNOldToNew.add(configPath + configJSONGenDirPath);
        JOSNOldToNew.add(GameUtils.getPropertyValue(anActionEvent, Constants.CONFIG_JSON_EFFECT_PATH_PRO_NAME));

        List<String> JAVAOldToNew = new ArrayList<>();
        JAVAOldToNew.add(configPath + configJAVAGenDirPath);
        JAVAOldToNew.add(GameUtils.getPropertyValue(anActionEvent, Constants.CONFIG_JAVA_EFFECT_PATH_PRO_NAME));

        oldToNewFilePaths.add(JOSNOldToNew);
        oldToNewFilePaths.add(JAVAOldToNew);

        return oldToNewFilePaths;
    }

}
